package com.lianrui.core;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianrui.commons.Page;
import com.lianrui.commons.ResultView;
import com.lianrui.commons.exception.BusinessException;

/**
 * 功能描述：控制器抽象类
 *
 * @author LJQ
 *
 * @param <P> 分页类型
 * @param <VO> 视图类型
 * @param <D> 实体类型
 * @param <T> 主键类型
 * 2015年12月21日 下午3:04:25
 */
@Controller
@SuppressWarnings("unchecked")
public abstract class AbstractController<P extends Page<D>, D extends Domain<T>, T> implements IController<P, D, T> {

	@RequestMapping("/add")
	@ResponseBody
	public ResultView<T> add(D d) {
		ResultView<T> view = new ResultView<T>();
		try {
			getService().add(d);
			view.setResult(d.getId());
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultView<Void> delete(@PathVariable T... id) {
		ResultView<Void> view = new ResultView<Void>();
		try {
			getService().delete(id);
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}

	@RequestMapping("/update")
	@ResponseBody
	public ResultView<Void> update(D d) {
		ResultView<Void> view = new ResultView<Void>();
		try {
			getService().update(d);
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}

	@RequestMapping("/unique/{id}")
	@ResponseBody
	public ResultView<D> unique(@PathVariable T id) {
		ResultView<D> view = new ResultView<D>();
		try {
			view.setResult(getService().unique(id));
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}

	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultView<List<D>> get(@PathVariable T... id) {
		ResultView<List<D>> view = new ResultView<List<D>>();
		try {
			view.setResult(getService().get(id));
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}

	@RequestMapping("/list")
	@ResponseBody
	public P list(P page) {
		try {
			getService().list(page);
		} catch (BusinessException e) {
			page.setCode(e.getCode());
			page.setMessage(e.getMessage());
		}
		return page;
	}
	
	@RequestMapping("/count")
	@ResponseBody
	public ResultView<Integer> count(P page) {
		ResultView<Integer> view = new ResultView<Integer>();
		try {
			view.setResult(getService().count(page));
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}
	
	@RequestMapping("")
	public String init() {
		return "index";
	}
	
	public abstract Logger getLogger();
	
	/**
	 * 功能说明：默认表单类型转换
	 * @param binder
	 * @throws Exception
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) throws Exception {
		//日期格式化
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    CustomDateEditor dateEditor = new CustomDateEditor(df, true);
	    binder.registerCustomEditor(Date.class, dateEditor);
	}
}
